const { app, BrowserWindow,Menu } = require('electron')
const path = require('path')

function createMenu() {
	const template = [
	  {
		label: '文件',
		submenu: [
		  {
			label: '新建',
			accelerator: 'CmdOrCtrl+N',
			role: 'new'
		  },
		  {
			label: '打开',
			accelerator: 'CmdOrCtrl+O',
			role: 'open'
		  },
		  {
			label: '退出',
			accelerator: 'CmdOrCtrl+Q',
			click: () => {
			  app.quit();
			}
		  }
		]
	  },
	  {
		label: '编辑',
		submenu: [
		  { label: '撤销', accelerator: 'CmdOrCtrl+Z', role: 'undo' },
		  { label: '重做', accelerator: 'Shift+CmdOrCtrl+Z', role: 'redo' },
		  { type: 'separator' },
		  { label: '剪切', accelerator: 'CmdOrCtrl+X', role: 'cut' },
		  { label: '复制', accelerator: 'CmdOrCtrl+C', role: 'copy' },
		  { label: '粘贴', accelerator: 'CmdOrCtrl+V', role: 'paste' },
		  { label: '删除', role: 'delete' },
		  { label: '全选', accelerator: 'CmdOrCtrl+A', role: 'selectall' }
		]
	  },
	  {
		label: '刷新',
		submenu: [
		  { label: '撤销', accelerator: 'CmdOrCtrl+Z', role: 'undo' },
		  { label: '重做', accelerator: 'Shift+CmdOrCtrl+Z', role: 'redo' },
		  { type: 'separator' },
		  { label: '剪切', accelerator: 'CmdOrCtrl+X', role: 'cut' },
		  { label: '复制', accelerator: 'CmdOrCtrl+C', role: 'copy' },
		  { label: '粘贴', accelerator: 'CmdOrCtrl+V', role: 'paste' },
		  { label: '删除', role: 'delete' },
		  { label: '全选', accelerator: 'CmdOrCtrl+A', role: 'selectall' }
		]
	  }
	];
   
	const menu = Menu.buildFromTemplate(template);
	Menu.setApplicationMenu(menu);
  }
 
function createWindow() {
	// Create the browser window.
	const mainWindow = new BrowserWindow({
		width: 800,
		height: 600,
		webPreferences: {
			preload: path.join(__dirname, 'preload.js'),
			nodeIntegration: true,
			contextIsolation: false,
			// 设置CSP策略
			// https://gitee.com/cao779840/vue3-admin.git
			// https://gitee.com/cao779840/cc.git
		  
		},
		autoHideMenuBar: false,  // 自动隐藏菜单栏, 除非按了Alt键
		// skipTaskbar: true    // 不显示在任务栏
		
	
	})
 
	// and load the index.html of the app.
	mainWindow.loadFile('dist/index.html')
  // mainWindow.loadURL('http://localhost:3000/')
 
	// Open the DevTools.
	mainWindow.webContents.openDevTools()
}
 

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createMenu);
app.whenReady().then(() => {
	createWindow()
 
	app.on('activate', function () {
		// On macOS it's common to re-create a window in the app when the
		// dock icon is clicked and there are no other windows open.
		if (BrowserWindow.getAllWindows().length === 0) createWindow()
	}
)
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
	if (process.platform !== 'darwin') app.quit()
})

